var $table = $("#table_object");

$(document).ready(function () {
    $.jgrid.defaults.styleUI = 'Bootstrap';
    $table
    .jqGrid({
            mtype:"post",
            url:"/object/data",
            datatype: "json",//为local时初始化不加载，支持json，xml等
            height: 350,
            autowidth: true,
            shrinkToFit: true,
            rowNum: 10,//每页记录数
            rowList: [10, 20, 30],//每页记录数可选列表
            loadonce:true,//一次性加载所有数据，用于前端分页
            colNames: ['编号', '名称', '能力', '实体关系', '特点', '操作'],//表头
            colModel: [//这里会根据index去解析jsonReader中root对象的属性，填充cell
                {name: 'objectId', index: 'objectId', width: 30, sorttype: "int"},
                {name: 'objectName', index: 'objectName', width: 50},
                {name: 'abilities', index: 'abilities', width: 80},
                {name: 'objRelationship', index: 'objRelationship', width: 30},
                {name: 'chara', index: 'chara',width: 120},
                {name: '操作', label: '操作',width: 40,
                    formatter:
                    function(cellvalue, options, row){
                        return"<a style='margin:2px' class=\'btn btn-primary btn-sm \' href=\'#\' title=\'编辑\'>" +
                            "<i class=\"fa fa-edit\">" +
                            "</i>" +
                            "</a>" +
                            "<a style='margin:2px' class=\"btn btn-danger btn-sm \" href=\"#\" title=\"删除\">" +
                            "<i class=\"fa fa-remove\">" +
                            "</i>" +
                            "</a>"
                    }
                }
            ],
/*        gridComplete: function () {
            var ids = $table.jqGrid('getDataIDs');
            for (var i = 0; i < ids.length; i++) {
                var cl = ids[i];
                ee = "<a style='cursor:pointer;' onclick=\"editDlg('"
                    + cl + "');\" ><i class='fa fa-edit'></i></a>";
                $table.jqGrid('setRowData', cl, { act: ee});
            }
        },*/
            pager: "#pager_object",
            viewrecords: true,//显示记录数信息,默认显示为{0}-{1} 共{2}条
            caption: "表2",//显示查询结果表格标题
            hidegrid: false,
            multiselect: true,//多选框
            multiboxonly: true,//在点击表格row时只支持单选，只有当点击checkbox时才多选，需要multiselect=true是有效
            toolbar: [true, "top"]
        })
        .navGrid('#pager_object',{edit:false,add:false,del:false,search:false,refresh:false},{height:200});
    $("#t_table_object").append(
        "<div class=\"input-group\" style=\"float: left;width:240px;margin:0 2px\">\n" +
        "<input id=\"find_id\" name=\"find_id\" type=\"text\" class=\"form-control\"/>\n" +
        "<span class=\"input-group-btn\">\n" +
        "<button class=\"btn btn-success\" type=\"button\" onclick=\"layer.msg('未实现')\"><i aria-hidden=\"true\" class=\"fa fa-search\"></i>查询</button>\n" +
        "</span>\n" +
        "</div>" +
        "<div style=\"float:right;margin:0 2px\">" +
        "<button id=\"add\" type=\"button\" class=\"btn btn-warning\" onclick=\"addDlg()\"><i aria-hidden=\"true\" class=\"fa fa-plus\"></i>增加</button>"+
        "</div>" +
        "<div style=\"float:right;margin:0 2px\">" +
        "<button id=\"delete\" type=\"button\" class=\"btn btn-danger\" onclick=\"layer.msg('有点问题')\"><i aria-hidden=\"true\" class=\"fa fa-trash\"></i>删除</button>" +
        "</div>" +
        // "<div style=\"float:right;margin:0 2px\">" +
        // "<button id=\"edit\" type=\"button\" class=\"btn btn-warning\" onclick=\"editDlg()\"><i aria-hidden=\"true\" class=\"fa fa-edit\"></i>修改</button>" +
        // "</div>" +
        "<div style=\"float:right;margin:0 2px\">" +
        "<button id=\"refresh\" type=\"button\" class=\"btn btn-info\" onclick=\"refDlg()\"><i aria-hidden=\"true\" class=\"fa fa-refresh\"></i>刷新</button>" +
        "</div>"
    );

    $(window).bind('resize', function () {
        var width = $('.jqGrid_wrapper').width();
        $table.setGridWidth(width);
    });
});




function test11() {
    alert("你好！！！");
}



//未选中值时会执行，导致问题！
function delDlg() {
    var rowIds = $table.jqGrid('getGridParam', 'selarrrow');
    layer.confirm("确认要删除选中的数据吗?", {
            btn : [ '确定', '取消' ]},
        function () {
            for(var i=0;i<rowIds.length;i++) {
                var rowid = rowIds[i];
                var row = $table.jqGrid('getRowData', rowid);
                var id = row.objectId;
                $.ajax({
                    type: "post",
                    url: "/object/delete",
                    data: {"id": id},
                    dataType: "text",
                    success: function (data) {
                        layer.msg(data);
                        reload();
                        layer.closeAll('dialog');
                    }
                });
            }
        }
    );
}


function refDlg() {
    reload();
}

function reload() {
    $table.jqGrid('clearGridData');
    $table.jqGrid('setGridParam',{  // 重新加载数据
        datatype:'json',
        mtype:"post",
        url:"/object/data"
    }).trigger("reloadGrid");
}